package com.work.service.impl;

import com.work.bean.Notice;
import com.work.bean.Page;
import com.work.dao.NoticeDao;
import com.work.dao.impl.NoticeDaoImpl;
import com.work.service.NoticeService;

import java.util.List;

public class NoticeServiceImpl implements NoticeService {
    NoticeDao noticeDao = new NoticeDaoImpl();
    public List<Notice> queryNotice() {
        return noticeDao.queryNotice();
    }

    public Notice queryNoticeById(Integer id) {
        return noticeDao.queryNoticeById(id);
    }

    public int saveNotice(Notice notice) {
        return  noticeDao.saveNotice(notice);
    }

    public int updateNotice(Notice notice) {
        return noticeDao.updateNotice(notice);
    }

    public int deleteNotice(Integer id) {
        return noticeDao.deleteNotice(id);
    }

    @Override
    public Page<Notice> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Notice> page = new Page<Notice>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = noticeDao.queryForPageTotalCountByPrice(min,max);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Notice> items = noticeDao.queryForPageItemsByPrice(begin,pageSize,min,max);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Notice> page(int pageNo, int pageSize) {
        Page<Notice> page = new Page<Notice>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = noticeDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize >0){
            pageTotal+=1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1)*pageSize;
        List<Notice> items = noticeDao.queryForPageItems(begin,pageSize);
        System.out.println(begin);
        System.out.println(pageSize);
        System.out.println("66666"+items);
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Notice> page(int pageNo, int pageSize, int min, int max) {
        Page<Notice> page = new Page<Notice>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = noticeDao.queryForPageTotalCountByPrice(min, max);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Notice> items = noticeDao.queryForPageItemsByPrice(begin, pageSize, min, max);
        page.setItems(items);
        return page;
    }


}
